<?php
class RatesController extends AppController {
    public $name    = 'Rates';
    public $helpers = array('Form');
    public $scaffold;

    public function rest_list ($ext='xml') {
        $data = $this->Rate->find('all');
        $this->rest_render($ext, $data);
    } // rest_list


    public function listing ($type=null) {
        $json = $this->Rate->rateList($this->params['form']);
        $this->response($json);
    } // listing

    public function save () {
        $this->data['Rate'] = $this->params['form'];

        if (!$this->data['Rate'] || count($this->data['Rate']) == 0) { // check if data post
            $json = array(
                'status' => false,
                'mesg'   => __('No data is posted!', true)
            );

            $this->response($json);
            return;
        }

        foreach ($this->data['Rate'] as $fieldname=>$value) { // convert empty data to null
            if (empty($value) && $value != '0') {
                $this->data['Rate'][$fieldname] = null;
            }
        }

        if ($this->data['Rate']['rr-room_type_id']) {
            $this->data['Rate']['room_type_id'] = $this->data['Rate']['rr-room_type_id'];
        }
        if ($this->data['Rate']['from']) {
            list($d, $m, $y) = split('[-/.]', $this->data['Rate']['from']);
            $this->data['Rate']['from'] = $y.'-'.$m.'-'.$d;
        }
        if ($this->data['Rate']['to']) {
            list($d, $m, $y) = split('[-/.]', $this->data['Rate']['to']);
            $this->data['Rate']['to'] = $y.'-'.$m.'-'.$d;
        }

        $this->Rate->set($this->data);

        if (!$this->Rate->validates()) { // invalid data found!
            $json = array(
                'status' => false,
                'mesg'   => __('Please enter the details in the fields pointed out above!', true)
            );

            foreach ($this->Rate->invalidFields() as $k=>$v) {
                $json['error'][] = array(
                    'f' => $k,
                    'm' => $v
                );
            }

            $this->response($json);
            return;
        }

        if (!$this->Rate->save($this->data)) { // error when saving data
            $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database, please try again.', true) : __('Fail update record to database, please try again.', true);
            $err  = ($this->error) ? $this->error : $mesg;
            $json = array(
                'status' => false,
                'mesg'   => $err
            );

            $this->response($json);
            return;
        }

        $mesg = empty($this->params['form']['id']) ? __('New rate has been created successfully!',true) : __('Rate has been updated successfully!',true);
        $json = array(
            'status' => true,
            'mesg'   => $mesg
        );

        $this->response($json);
    } // save
} // RatesController
?>